/*
 * ********************************************************************************
 * Project:        GAMES, 7th Framework Programme, co-funded by European Commission
 * Product:        ESMI Prototype
 * File:           CorrelationMiner.java (UTF-8)
 * Reviewd by:     Alessandro Rossi <alessandro.rossi@eng.it>
 * Reviewd on:     11-giu-2012 15.32.21
 * 
 * (C) Copyright 2010-2012 Engineering Ingegneria Informatica S.p.A.
 * 
 * This file is part of the Energy Sensing & Monitoring Infrastructure of GAMES.
 * ********************************************************************************
 * 
 * The terms of the license are under discussion and will be defined.
 * Until then, the distribution of this file is restricted to the members of the GAMES Consortium.
 */
package eu.games.esmi.ieat.indicatorrelations.data;

/**
 *
 * @author Alessandro
 */
public class CorrelationMiner {
    public static double getPearsonCorrelation(double[] scores1,double[] scores2){

        double mean_x = 0;
        double mean_y = 0;
        double sd_x = 0;
        double sd_y = 0;
        double sum_of_prod = 0;
        double cov_x_y = 0;
        int size = scores1.length;
        for (int i=0; i<size; i++){
                mean_x += scores1[i];
                mean_y += scores2[i];
        }
        mean_x = mean_x/size;
        mean_y = mean_y/size;

        for (int i=0; i<size; i++){
                sd_x += (scores1[i] - mean_x)*(scores1[i] - mean_x);
                sd_y += (scores2[i] - mean_y)*(scores2[i] - mean_y);
                sum_of_prod += scores1[i]*scores2[i];
        }

        sd_x = (double) Math.sqrt(sd_x/size);
        sd_y = (double) Math.sqrt(sd_y/size);
        sum_of_prod = sum_of_prod/size;

        cov_x_y = sum_of_prod - mean_x * mean_y;

        return cov_x_y == 0 ? 0 : cov_x_y/(sd_x*sd_y);
    }
}
